﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace B.T.BasicData.Write_Api.Globalexceptionfilter
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class Globalexceptionfilter : IExceptionFilter
    {
        private readonly ILogger<Globalexceptionfilter> logger;

        public Globalexceptionfilter(ILogger<Globalexceptionfilter> logger)
        {
            this.logger = logger;
        }


        /// <summary>
        /// 异常报错执行该方法
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnException(ExceptionContext context)
        {
            try
            {
                logger.LogError("全局异常报错了",context.Exception.Message);

                context.ExceptionHandled = true;
            }
            catch (Exception ex)
            {

                logger.LogError("异常" + ex.Message);
                throw ex;
            }
        }
    }
}
